// ClassInfoRepository.java
package com.example.orderfood2.Repository;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class ClassInfoRepository extends BaseRepository {
    
    public ClassInfoRepository(Context context) {
        super(context);
    }

    public long insertClass(String classNumber, String className) {
        open();
        ContentValues values = createContentValues();
        values.put("classnumber", classNumber);
        values.put("classname", className);
        long result = database.insert("class_info", null, values);
        close();
        return result;
    }

    public List<String> getAllClassNumbers() {
        open();
        List<String> classNumbers = new ArrayList<>();
        Cursor cursor = database.rawQuery("SELECT classnumber FROM class_info", null);
        
        if (cursor != null) {
            try {
                while (cursor.moveToNext()) {
                    classNumbers.add(cursor.getString(0));
                }
            } finally {
                cursor.close();
            }
        }
        close();
        return classNumbers;
    }

    //    class_info的添加
    public long InsertClass(String classNumber, String className) {
        open();
        ContentValues class_info = new ContentValues();
        class_info.put("classnumber", classNumber);
        class_info.put("classname", className);
        long result = database.insert("class_info", null, class_info);
//        插入操作失败，该方法会返回 -1
        close();
        return result;
    }

    //    管理员的class_info的查询
    public List<String> QueryClassInfo() {
        List<String> classNumbers = new ArrayList<>();
        open();
        Cursor classInfo = null;
        try {
            classInfo = database.query("class_info", null, null, null, null, null, null);
            if(classInfo!=null){
                while (classInfo.moveToNext()){
                    String number=classInfo.getString(1);
                    classNumbers.add(number);
                }
            }
        } catch (Exception e) {
            Log.e("DBUtil", "查询班级信息失败", e);
        } finally {
            if (classInfo != null) {
                classInfo.close();
            }
            database.close();
        }
        close();
        return classNumbers;
    }

    //老师的class_info的查询
    public List<String> TeacherQueryClassInfo(String science){
        List<String> classNumbers=new ArrayList<>();
        Cursor classInfo=null;
        open();
        try {
            classInfo = database.query("class_info", null, "classname=?", new String[]{science}, null, null, null);
            if(classInfo!=null){
                while (classInfo.moveToNext()){
                    String number=classInfo.getString(1);
                    classNumbers.add(number);
                }
            }
        } catch (Exception e) {
            Log.e("DBUtil", "查询班级信息失败", e);
        } finally {
            if (classInfo != null) {
                classInfo.close();
            }
            database.close();
        }
        close();
        return classNumbers;
    }
    public Cursor getAllClasses(){
        open();
        Cursor cursor = database.rawQuery("SELECT id, classnumber FROM class_info", null);
//        close();
        return cursor;
    }
    //    class_info的删除
    public boolean deleteClass(int id) {
        open();
        boolean classInfo = database.delete("class_info", "id=?", new String[]{String.valueOf(id)}) > 0;

        close();
        return classInfo;
    }
    //通过id查询具体的class
    public Cursor getClassById(int id) {
        open();
        Cursor cursor = database.rawQuery("SELECT * FROM class_info WHERE id=?", new String[]{String.valueOf(id)});
//        close();
        return cursor;
    }
    //    class_info的修改
    public boolean updateClass(int id, String newNumber, String newName) {
        open();
        ContentValues values = new ContentValues();
        values.put("classnumber", newNumber);
        values.put("classname", newName);
        boolean classInfo = database.update("class_info", values, "id=?", new String[]{String.valueOf(id)}) > 0;
        close();
        return classInfo;
    }
}